//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef ___DISPLAY_DEF_H__
#define ___DISPLAY_DEF_H__

#include <display.h>

////////////////////////////////////////////////////////////////////////////////
//
// Debug information control
//
//#define DEBUG_DISPLAY
#ifdef DEBUG_DISPLAY

//#define OVERLAY2_CAN_REENABLE // Overlay2 can be reenabled
                                // without disabling it

#define DBGOUT(m, x)  if ((m)) kprintf x
#define DBGOUT_FUNC(m, x) if ((m)) x
#define DBG_ERR     1
#define DBG_LOG     1
#define DBG_DUMP    1
#define DBG_POWER   1
#define DBG_OVERLAY 1
#define DBG_CTLCODE 1
#else
#define DBGOUT(m, x)
#define DBGOUT_FUNC(m, x)
#endif

#define LCD_WIDTH    240
#define LCD_HEIGHT   320
#define LCD_BPP_DIS  16 // bit per pixel

#define BASEPLANE_COLORFORMAT   COLOR_RGB565  // default
#define BASEPLANE_PIXELBYTES    2
#define BASEPLANE_PIXELBITS     16

#define OVERLAY1_PIXELBYTES 4

#define OVERLAYTYPE_MASK     0x1
#define OVERLAYTYPE_UNDERLAY 0
#define OVERLAYTYPE_OVERLAY  1

#define __PAGESIZE 0x1000
#define ROUNDUP_PAGE(n)     (((n) + (__PAGESIZE) - 1) & ~((__PAGESIZE) - 1))

#define DESC_SIZE            (0x20)
#define BASEPLANE_FB_SIZE    \
    ROUNDUP_PAGE((LCD_WIDTH * LCD_HEIGHT * BASEPLANE_PIXELBYTES))
#define OVERLAY1_FB_MAXSIZE \
    ROUNDUP_PAGE((LCD_WIDTH * LCD_HEIGHT * OVERLAY1_PIXELBYTES))
#define OVERLAY2Y_FB_MAXSIZE    \
    ROUNDUP_PAGE((LCD_WIDTH * LCD_HEIGHT))  // Max size of Y Chan
#define OVERLAY2Cb_FB_MAXSIZE   \
    ROUNDUP_PAGE((LCD_WIDTH * LCD_HEIGHT / 2))  // Max size of Cb Chan
#define OVERLAY2Cr_FB_MAXSIZE  OVERLAY2Cb_FB_MAXSIZE   // Max size of Y Chan
#define OVERLAY2TOTAL_FB_MAXSIZE \
    (OVERLAY2Y_FB_MAXSIZE + OVERLAY2Cb_FB_MAXSIZE + OVERLAY2Cr_FB_MAXSIZE)

#define BASEPLANE_FB_PAGES (BASEPLANE_FB_SIZE >> PAGE_SHIFT)
#define OVERLAY1_FB_PAGES (OVERLAY1_FB_MAXSIZE >> PAGE_SHIFT)
#define OVERLAY2_FB_PAGES (OVERLAY2TOTAL_FB_MAXSIZE >> PAGE_SHIFT)

typedef struct
{

    // Physical address of frame buffer
    unsigned int paBasePlane ;
    unsigned int paOverlay1  ;
    unsigned int paOverlay2Y ;
    unsigned int paOverlay2Cb;
    unsigned int paOverlay2Cr;
    unsigned int paPalette;
    unsigned int paBasePlaneTemp ;
    
    // Virtual address of frame buffer
    unsigned int vaBasePlane ;
    unsigned int vaOverlay1  ;
    unsigned int vaOverlay2Y ;
    unsigned int vaOverlay2Cb;
    unsigned int vaOverlay2Cr;
    unsigned int vaPalette;
    unsigned int vaBasePlaneTemp ;

    // Physical address of DMA descriptors
    unsigned int paChan0Desc ; // Baseplane descriptor
    unsigned int paChan1Desc ; // Overlay1 channel descriptor
    unsigned int paChan2Desc ; // Overlay2 Y channel descriptor
    unsigned int paChan3Desc ; // Overlay2 Cb channel descriptor
    unsigned int paChan4Desc ; // Overlay2 Cr channel descriptor
    unsigned int paChan1AltDesc; // Overlay1 alterable channel descriptor
    unsigned int paPaletteDesc; // Palette descriptor

    // Virtual address of DMA descriptors
    unsigned int vaChan0Desc ; // Baseplane descriptor
    unsigned int vaChan1Desc ; // Overlay1 channel descriptor
    unsigned int vaChan2Desc ; // Overlay2 Y channel descriptor
    unsigned int vaChan3Desc ; // Overlay2 Cb channel descriptor
    unsigned int vaChan4Desc ; // Overlay2 Cr channel descriptor
    unsigned int vaChan1AltDesc; // Overlay1 alterable channel descriptor
    unsigned int vaPaletteDesc; // Palette descriptor

    // DMA transfer size
    unsigned int uChan0Size;
    unsigned int uChan1Size;
    unsigned int uChan2Size;
    unsigned int uChan3Size;
    unsigned int uChan4Size;

    // Baseplane
    ColorFormat basePlaneFormat;

    // Overlay
    OverlayFormat overlay[3]; // 0 reserved
        // 1 ~ 2 for Overlay1 & Overlay2

    int baseChangeRefCnt;
    int overlayType;

} DisplayContext;

//
// Functions
//

ECode Display_Create(DisplayContext *pCxt);
ECode Display_Init(DisplayContext *pCxt);
void *Display_GetBasePlane(DisplayContext *pCxt);
ECode Display_SetOverlay(DisplayContext *pCxt, OverlayFormat *pOverlay);
void Display_DisableOverlay(DisplayContext *pCxt, int layer);
void Display_GetOverlayFormat(DisplayContext *pCxt, int layer,
                OverlayFormat *pOverlay);
ECode Display_ChangeBasePlaneFormat(DisplayContext *pCxt, ColorFormat format);
void Display_Suspend(DisplayContext *pCxt, int type, int lcdOff);
void Display_Resume(DisplayContext *pCxt, int lcdOn);

void LcdCtrl_SetTransLevel(DisplayContext *pCxt, Byte level);
void LcdCtrl_Init(DisplayContext *pCxt);
void LcdCtrl_Enable(DisplayContext *pCxt);
void LcdCtrl_Disable(DisplayContext *pCxt);
ECode LcdCtrl_SetOverlay(DisplayContext *pCxt, OverlayFormat *pOverlay);
void LcdCtrl_DisableOverlay(DisplayContext *pCxt, int layer);
void LcdCtrl_Suspend(DisplayContext *pCxt, int type);
void LcdCtrl_Resume(DisplayContext *pCxt);
void LcdCtrl_OverlayResetController(DisplayContext *pCxt, OverlayFormat *pOverlay);

void Lcm_SetupGPIOs(DisplayContext *pCxt);
void Lcm_DisplayOn(DisplayContext *pCxt, int LcmPowerOn);
void Lcm_DisplayOff(DisplayContext *pCxt);

void BackLight_Open(DisplayContext *pCxt);
void BackLight_Close(DisplayContext *pCxt);
int BackLight_SetBrightness(DisplayContext *pCxt, int b);

#endif // ___DISPLAY_DEF_H__
